/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epam.expertsys.dbconn;

import com.epam.expertsys.blogic.Request;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author Admin
 */
public class RequestDAOH2 extends GenericDAOH2 implements RequestDAOIface {

    private static Logger log = Logger.getLogger(RequestDAOH2.class);

    public RequestDAOH2(int maxConnCount) throws SQLException {
        super(maxConnCount);
        super.start();
    }

    @Override
    public void addRequest(Request newRequest) throws SQLException {
        Connection conn = null;
        PreparedStatement prepStat = null;


        Long patientId = newRequest.getPatientId();
        Long requestId = newRequest.getRequestId();
        Long requestDate = newRequest.getRequestDate();
        String symptomsDescription = newRequest.getSymptomsDescription();
        String assumedSpecialization = newRequest.getAssumedSpecialization();

        try {
            conn = conPool.getConnection();
            prepStat = conn.prepareStatement("Insert into request "
                    + "(REQUEST_DATE, SYMPTOMS_DESCRIPTION, PATIENT_ID, "
                    + "ASSUMED_SPECIALIZATION) values (?, ?, ?, ?)");

            /*if (requestId != null) {
            prepStat.setLong(1, requestId);
            } else {
            prepStat.setNull(1, java.sql.Types.BIGINT);
            }*/

            if (requestDate != null) {
                prepStat.setLong(1, requestDate);
            } else {
                prepStat.setNull(1, java.sql.Types.BIGINT);
            }

            if (symptomsDescription != null) {
                prepStat.setString(2, symptomsDescription);
            } else {
                prepStat.setNull(2, java.sql.Types.VARCHAR);
            }

            if (patientId != null) {
                prepStat.setLong(3, patientId);
            } else {
                prepStat.setNull(3, java.sql.Types.BIGINT);
            }

            if (assumedSpecialization != null) {
                prepStat.setString(4, assumedSpecialization);
            } else {
                prepStat.setNull(4, java.sql.Types.VARCHAR);
            }

            prepStat.executeUpdate();

            //if (resultSet.)


        } catch (SQLException e) {
            System.out.println("SQL error");
            throw new SQLException(e);
        } finally {
            closeStatement(prepStat);
            closeConnection(conn);
        }

        log.info("SQL: New request added");
    }

    @Override
    public List<Request> getRequestsByLogin(String login) {
        List<Request> result = new ArrayList<Request>();

        Connection conn = null;
        PreparedStatement prepStat = null;


        try {
            conn = conPool.getConnection();
            prepStat = conn.prepareStatement("Select * from Request where "
                    + "patient_id = (select patient_id from patient where login = ?)");

            prepStat.setString(1, login);

            ResultSet resultSet = prepStat.executeQuery();

            while (resultSet.next()) {
                Long patientId = resultSet.getLong("PATIENT_ID");
                if (resultSet.wasNull()) {
                    patientId = null;
                }


                String symptomsDescription = resultSet.getString("SYMPTOMS_DESCRIPTION");
                if (resultSet.wasNull()) {
                    symptomsDescription = null;
                }

                String assumedSpecialization = resultSet.getString("ASSUMED_SPECIALIZATION");
                if (resultSet.wasNull()) {
                    assumedSpecialization = null;
                }

                Long requestId = resultSet.getLong("REQUEST_ID");
                if (resultSet.wasNull()) {
                    requestId = null;
                }

                Long requestDate = resultSet.getLong("REQUEST_DATE");
                if (resultSet.wasNull()) {
                    requestDate = null;
                }

                result.add(new Request(requestId, requestDate, symptomsDescription,
                        patientId, assumedSpecialization));
            }



        } catch (SQLException e) {
            log.error("SQL error");
            e.printStackTrace();
        } finally {
            closeStatement(prepStat);
            closeConnection(conn);
        }
        return result;
    }
}
